﻿using System;
using System.Collections.Generic;
using System.Text;
using NetworkLib.Utilities;

namespace PokemonBattle.RoomList.Client
{
    public static class RoomListClientHelper
    {

        public static ByteArray RoomLogon(Room roomInfo)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomListMethods.Server_RoomLogon_Hash);
            roomInfo.WriteToByteArray(byteArray);


            return byteArray;
        }

        public static ByteArray RoomLogout()
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomListMethods.Server_RoomLogout_Hash);

            return byteArray;
        }

        public static ByteArray UpdateRoomInfo(Room roomInfo)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomListMethods.Server_UpdateRoomInfo_Hash);
            roomInfo.WriteToByteArray(byteArray);


            return byteArray;
        }

        public static ByteArray UserLogon()
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomListMethods.Server_UserLogon_Hash);

            return byteArray;
        }

        public static ByteArray UserLogout()
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomListMethods.Server_UserLogout_Hash);

            return byteArray;
        }


        public static bool InterpretMessage(int sessionID, ByteArray byteArray, IRoomListClientService clientService)
        {
            byteArray.BypassHeader();
            int methodID = byteArray.ReadInt();

            switch (methodID)
            {

                case EnumRoomListMethods.Client_LogonSuccess_Hash:
                    return OnLogonSuccess(byteArray, clientService);

                case EnumRoomListMethods.Client_LogonFail_Hash:
                    return OnLogonFail(byteArray, clientService);

                case EnumRoomListMethods.Client_UpdateRoom_Hash:
                    return OnUpdateRoom(byteArray, clientService);

                case EnumRoomListMethods.Client_AddNewRoom_Hash:
                    return OnAddNewRoom(byteArray, clientService);

                case EnumRoomListMethods.Client_RemoveRoom_Hash:
                    return OnRemoveRoom(byteArray, clientService);

                case EnumRoomListMethods.Client_AddRoomList_Hash:
                    return OnAddRoomList(byteArray, clientService);

            }

            byteArray.Rewind();
            return false;
        }

        private static bool OnLogonSuccess(ByteArray byteArray, IRoomListClientService clientService)
        {

            clientService.OnLogonSuccess();
            return true;
        }

        private static bool OnLogonFail(ByteArray byteArray, IRoomListClientService clientService)
        {
            string message = byteArray.ReadUTF();

            clientService.OnLogonFail(message);
            return true;
        }

        private static bool OnUpdateRoom(ByteArray byteArray, IRoomListClientService clientService)
        {
            Room roomInfo = new Room();
            roomInfo.ReadFromByteArray(byteArray);

            clientService.OnUpdateRoom(roomInfo);
            return true;
        }

        private static bool OnAddNewRoom(ByteArray byteArray, IRoomListClientService clientService)
        {
            Room roomInfo = new Room();
            roomInfo.ReadFromByteArray(byteArray);

            clientService.OnAddNewRoom(roomInfo);
            return true;
        }

        private static bool OnRemoveRoom(ByteArray byteArray, IRoomListClientService clientService)
        {
            int identity = byteArray.ReadInt();

            clientService.OnRemoveRoom(identity);
            return true;
        }

        private static bool OnAddRoomList(ByteArray byteArray, IRoomListClientService clientService)
        {
            RoomSequence rooms = new RoomSequence();
            rooms.ReadFromByteArray(byteArray);

            clientService.OnAddRoomList(rooms);
            return true;
        }

    }
}
